package interfaces;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;

public class DynamicProxyTest1<E> extends ArrayList<E> implements List<E>{
    public static void main(String[] args) {
        DynamicProxyTest1 d = new DynamicProxyTest1();
        DynamicProxyTest1_Proxy p = new DynamicProxyTest1_Proxy(d);
        List l = (List) p.getProxy();
        System.out.println(l);
    }
}

class DynamicProxyTest1_Proxy implements InvocationHandler {

    Object o = null;

    public DynamicProxyTest1_Proxy(Object o) {
        this.o = o;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("先执行代理方法");
        Object result = method.invoke(o, args);
        return result;
    }

    public Object getProxy() {
        return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), o.getClass().getInterfaces(),
                this);
    }
}